#include "stddef.h"
#include "string.h"
#include "cpuid.h"

/* There is currently no optimized backwards memcpy.  It's on the ToDo list. */

static const void * memmove_std(const void * const dst, const void * const src, const size_t m_count)
{
	/* move memory starting from the front of the stream, to the end */
	if((size_t)src > (size_t)dst)
	{ memcpy(dst, src, m_count); }
	/* move memory starting from the end of the stream, to the front */
	else
	{
		/* chuckle chuckle chuckle... */
		asm("std; rep movsb;"::"S"((size_t)src + m_count), "D"((size_t)dst + m_count), "c"(m_count));
	}

	/* and this makes it a real memMOVE instead of just a memcpy clone */
	memclr((void*)((size_t)dst + m_count), ((size_t)src + m_count) - ((size_t)dst + m_count));

	return dst;
}

const void * memmove(const void * const dst, const void * const src, const size_t count)
{
	/* save us some time if the programmer screwed up! */
	if(!count){return 0;}

	/* no sense in continuing if we're just moving over ourselves */
	if((size_t)src == (size_t)dst){ return 0; }

	return memmove_std(dst, src, count);
}

